﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
            {

            }
            else
            {
                FillData();
            }

        }
        private void FillData()
        {
            var sql = "select id,username 用户名,password 密码,remarks 备注 from users";
            var dt = DbHelper.GetData(sql);

            gvl.DataSource = dt;
            gvl.DataBind();
        }

        protected void gvl_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvl.EditIndex = -1;
        }

        protected void gvl_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvl_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvl.EditIndex = e.NewEditIndex;
        }

        protected void gvl_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 1);
            var username = GetCellValue(e.RowIndex, 2);
            var password = GetCellValue(e.RowIndex, 3);
            var remarks = GetCellValue(e.RowIndex, 4);
            var sql = string.Format("update Users set Username='{0}',Password='{1}',Remarks'{2}'where Id={3}",username,password,remarks,id);
            DbHelper.AddOrUpdateOrDelete(sql);
            gvl.EditIndex = -1;
            FillData();
        }
        public string GetCellValue(int rowIndex,int colIndex)
        {
            var control = gvl.Rows[rowIndex].Cells[colIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }
    }
}